package com.cory.admin.sys.controller.admin;

import com.alibaba.fastjson.JSON;
import com.cory.admin.sys.controller.AdminController;
import com.cory.admin.sys.service.SysAreaService;
import com.cory.boot.component.cache.RedisUtils;
import com.cory.boot.domain.bean.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 省市区地址管理 数据来源（京东地址库）
 *
 * @author lw
 * @version 1.0
 * @date 2022/1/23 14:00
 */
@Controller
@RequestMapping("admin/sys/area")
public class SysAreaController  extends AdminController {

    @Autowired
    private SysAreaService sysAreaService;

    private String redis_key = "sysAreaJson";

    /**
     * 获取地址Json数据
     * @return
     */
    @GetMapping("getJsonData")
    @ResponseBody
    public String getJsonData() {
        String jdAreaJson = RedisUtils.get(redis_key);
        if(StringUtils.isEmpty(jdAreaJson)) {
            Map<String,Object> jsonDataMap = sysAreaService.getCityJsonData();
            jdAreaJson = JSON.toJSONString(jsonDataMap);
            RedisUtils.set(redis_key,jdAreaJson);
        }
        return jdAreaJson;
    }

    /**
     * 重置地址Json Redis缓存
     * @return
     */
    @GetMapping("resetJsonData")
    @ResponseBody
    public Result<?> resetJsonData() {
        RedisUtils.del(redis_key);
        return Result.success();
    }

}
